<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $employee_name
 * @property string $level
 * @property string $status
 *
 * The followings are the available model relations:
 * @property QuotationOrderHeader[] $quotationOrderHeaders
 */
class User extends CActiveRecord
{

	public $sign;
	public $tempPassword;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, employee_name, level', 'required'),
			array('password', 'required', 'on'=>'insert'),
			array('tempPassword', 'length', 'max'=>100,),
            array('sign', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true,'wrongType'=>'Only  JPG and PNG allowed', 'on'=>'insert'),
            array('sign', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true,'wrongType'=>'Only  JPG and PNG allowed', 'on'=>'update'),
            array('email','email'),
			array('username', 'length', 'max'=>30),
			array('password', 'length', 'max'=>100, 'allowEmpty'=>true, 'on'=>'update'),
			array('password', 'length', 'max'=>100, 'allowEmpty'=>false, 'on'=>'insert'),
			array('employee_name', 'length', 'max'=>40),
			array('level, status', 'length', 'max'=>10),
			// The following rule is used by search().//7510d498f23f5815d3376ea7bad64e29
			// @todo Please remove those attributes that should not be searched.//d41d8cd98f00b204e9800998ecf8427e
			array('id, username, password, employee_name, level, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'quotationOrderHeaders' => array(self::HAS_MANY, 'QuotationOrderHeader', 'employee_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'USERNAME',
			'password' => 'PASSWORD',
			'tempPassword' => 'PASSWORD',
			'employee_name' => 'EMPLOYEE NAME',
			'level' => 'LEVEL',
			'status' => 'STATUS',
			'sign'=>'SIGN',
			'email'=>'EMAIL',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('employee_name',$this->employee_name,true);
		$criteria->compare('level',$this->level,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave() {

	    if (parent::beforeSave()) {

	    	$this->password = md5($this->password);
	        $this->status='Aktif';
	        
	        return true;
	    }
	}
}
